/**
 *
 */
package com.newer.controller.sys.role;

import com.newer.biz.sys.role.RoleInfoBiz;
import com.newer.commons.result.BizResult;
import com.newer.dao.bean.sys.Menu;
import com.newer.dao.bean.sys.Role;
import com.newer.dao.bean.sys.RolePermission;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 角色信息Controller
 *
 * @author xuntj
 * @create 2018-03-19 19:41
 **/
@Controller
@RequestMapping("sys/role")
public class RoleInfoController {

    /**
     * 角色信息业务操作类
     */
    @Autowired
    RoleInfoBiz roleInfoBiz;

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/list.htm")
    public String role() {
        return "sys/role/role_list";
    }

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/doList.do")
    @ResponseBody
    public String roleList(Role role) {
        BizResult<Role> bizResult = roleInfoBiz.queryRoleInfoByPage(role);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 查询角色菜单信息
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/doRoleMenuList.do")
    @ResponseBody
    public String roleMenusList(String roleId) {
        BizResult<Menu> bizResult = roleInfoBiz.queryRoleMenuInfos(roleId);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 查询角色权限信息
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/doPermissionList.do")
    @ResponseBody
    public String rolePermissionList(String roleId) {
        BizResult<RolePermission> bizResult = roleInfoBiz.queryRolePermissionByRoleId(roleId);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 添加角色信息页面渲染
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/roleOperate.htm")
    public String add(String roleId, ModelMap map) {
        if (StringUtils.isNotBlank(roleId)) {
            BizResult bizResult = roleInfoBiz.queryRoleInfoById(roleId);
            if (bizResult.isSuccess()) {
                map.put("role", bizResult.getResult());
            }
        }
        return "sys/role/role_operate";
    }


    /**
     * 菜单信息操作
     *
     * @return
     */
    @RequestMapping("/doOperate.do")
    @ResponseBody
    public String doOperate(@RequestParam(value = "permissionIds[]", required = false) List<String> permissionIds,
                            @RequestParam(value = "menuIds[]", required = false) List<String> menuIds,
                            @RequestParam(value = "permissionIds", required = false) String permissionId,
                            Role role) {
        // 查询所有的菜单信息
        BizResult<Role> bizResult = roleInfoBiz.operateRoleInfo(role,
                menuIds,
                CollectionUtils.isNotEmpty(permissionIds) ? permissionIds :
                        (StringUtils.isNotBlank(permissionId) ? Arrays.asList(permissionId) : null));
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }


    /**
     * 菜单信息操作
     *
     * @return
     */
    @RequestMapping("/doRoleMenuChecked.do")
    @ResponseBody
    public String doRoleMenuChecked(String roleId) {
        // 查询所有的菜单信息
        BizResult<Menu> bizResult = roleInfoBiz.queryRoleMenuCheckedInfos(roleId);
        // 返回结果
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 角色信息校验
     *
     * @return
     */
    @RequestMapping("/doCheck.do")
    @ResponseBody
    public String doCheck(String roleCode) {
        BizResult<?> bizResult = roleInfoBiz.checkRoleCodeExits(roleCode);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 角色信息校验
     *
     * @return
     */
    @RequestMapping("/validStatus.do")
    @ResponseBody
    public String doDel(Role role) {
        BizResult<?> bizResult = roleInfoBiz.delRoleById(role);
        return JSONObject.fromObject(bizResult).toString();
    }

}
